package com.example.smart_video.ui.activity;

import static com.example.smart_video.adapter.HBomeListAdapter.SCENE_SELECT_CLASS_LIST;
import static com.example.smart_video.adapter.HBomeListAdapter.SCENE_TEXT_LIST;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.GridLayoutManager;

import com.alibaba.fastjson.JSON;
import com.example.cleandemo3.R;
import com.example.cleandemo3.databinding.ActivityTextListBinding;
import com.example.smart_video.adapter.HBomeListAdapter;
import com.example.smart_video.model.Constants;
import com.example.smart_video.model.json.CommonRootBean;
import com.example.smart_video.model.json.VideoClassData;
import com.example.smart_video.model.json.VideoData;
import com.example.smart_video.utils.HttpUtils;
import com.wink_172.library.AppManager;
import com.wink_172.library.activity.SmartActivity;
import com.wink_172.library.callback.ICallback;
import com.wink_172.library.model.BaseDataItem;
import com.wink_172.library.utils.CommonUtils;
import com.wink_172.library.view.ActionBarView;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;
import java.util.SortedMap;

/**
 * 选择分类
 */
public class TextListActivity extends SmartActivity {

    public ActivityTextListBinding binding;

    public static void startActivity(Activity context, Object... args) {
        Intent intent = new Intent(context, TextListActivity.class);

//        intent.putExtra(Constants.PARAM_DATA1, (String) args[0]);
        context.startActivityForResult(intent, (Integer) args[0]);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        binding = DataBindingUtil.setContentView(this, R.layout.activity_text_list);

        initView();
        getClassList();
    }

    public void initView() {
        ((ActionBarView) binding.actionBar).updateAllContent(R.mipmap.icon_nav_arrow, "文案列表", "");
        ((ActionBarView) binding.actionBar).setCallback(new ActionBarView.ICallback() {
            @Override
            public void onSendEvent(int event, String content) {
                if (event == ActionBarView.LEFT_BTN) {
                    AppManager.Companion.getAppManager().finishActivity();

                } else if (event == ActionBarView.RIGHT_BTN) {
//ThumbListActivity.startActivity(getActivity(),-1);
                }
            }
        });


        List<Object> list = new ArrayList();
        HBomeListAdapter homeListAdapter = new HBomeListAdapter(this, list, binding.listView);
        binding.listView.init(homeListAdapter, false);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(), 4, GridLayoutManager.VERTICAL, false);
        gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
//                if (position < 7 || position > 14) {
//                    return 3;
//                }
                return 4;
            }
        });
        binding.listView.setLayoutManager(gridLayoutManager);

        homeListAdapter.setMOnItemClickListener(new HBomeListAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                HBomeListAdapter homeListAdapter = (HBomeListAdapter) binding.listView.getAdapter();
                List<Object> list = homeListAdapter.getDataList();
                BaseDataItem baseDataItem = (BaseDataItem) list.get(position);

                VideoData video_data = (VideoData) baseDataItem.getData();
                com.wink_172.library.utils.CommonUtils.copyStr(video_data.getContent());
                CommonUtils.showToast("复制成功");
                UploadVideoActivity.startActivity(getActivity(), video_data);



            }
        });
        binding.listView.setLayoutManager(gridLayoutManager);

    }

    private static final String TAG = "TextListActivity";


    /**
     * 获取文案列表
     */
    private void getClassList() {
        HttpUtils.INSTANCE.eventGetVideoList(null,
                new com.wink_172.library.callback.ICallback() {
                    @Override
                    public void onSendEvent(int event, @NonNull Object... objects) {
                        if (event == com.example.smart_video.model.Constants.EventEnum.网络请求成功.ordinal()) {
                            String result = (String) objects[0];
                            CommonRootBean commonRootBean = JSON.parseObject(result, CommonRootBean.class);

                            List videoDataList = JSON.parseArray(commonRootBean.getData(), VideoData.class);
                            initList(videoDataList);
                        }
                    }
                });
    }

    public void initList(List<VideoData> classList) {
        List<Object> list = new ArrayList<Object>();

        for (int i = 0; i < classList.size(); i++) {
            VideoData videoClassData = classList.get(i);

            if (TextUtils.isEmpty(videoClassData.getContent()) == false) {
                BaseDataItem baseDataItem = new BaseDataItem(i, SCENE_TEXT_LIST);
                baseDataItem.setObjectData(videoClassData);

                list.add(0,baseDataItem);
            }

        }

        HBomeListAdapter homeListAdapter = (HBomeListAdapter) binding.listView.getAdapter();
        homeListAdapter.swapContent(list);
        homeListAdapter.notifyDataSetChanged();
    }
}